//
//  HNSH_ExchangeViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/4/4.
//

#import "HNSH_ExchangeViewController.h"
#import "SDCycleScrollView.h"

#import "HNSH_ExchangeHuaFeiViewController.h"
#import "HNSH_ExchangeDianFeiViewController.h"
#import "HNSH_ExchangeYouKaViewController.h"
#import "HNSH_ExchangeRecordViewController.h"

#import "HNSH_HomeViewModel.h"
#import "HNSH_ExchangeNetServer.h"

@interface HNSH_ExchangeViewController ()<SDCycleScrollViewDelegate>

/* rightItemButton */
@property (nonatomic, strong) UIButton *rightItemButton;

/* cycleScrollView */
@property(nonatomic, strong) SDCycleScrollView *cycleScrollView;

@end

@implementation HNSH_ExchangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"购物值专区" showLeftItem:YES];
    
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    [self loadHomeScrollData];
    
    [self addAllViews];
}

- (void)addAllViews {
    [self configTopView];
    
    [self configBottomView];
}

- (void)configTopView {
    [self.view addSubview:self.rightItemButton];
    
    [self.view addSubview:self.cycleScrollView];
    self.cycleScrollView.layer.cornerRadius = 8.0f;
    self.cycleScrollView.layer.masksToBounds = YES;
    
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kNavBarAndStatusBarHeight+16);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(120);
    }];
}

- (void)configBottomView {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+152, kScreenWidth, 136)];
    bgView.layer.cornerRadius = 8.0f;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 19, 23)];
    imgV.image = [UIImage imageNamed:@"exchange_huo"];
    [bgView addSubview:imgV];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(48, 19, 200, 25)];
    titleLabel.text = @"热门应用";
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textColor = [UIColor appBlack333LabelColor];
    [bgView addSubview:titleLabel];
    
    NSArray *titles = @[@"话费快充",@"话费速充",@"电费充值"];
    NSArray *images = @[@"exchange_huafei",@"exchange_huafei",@"exchange_dianfei"];
//    CGFloat spece = (bgView.width-46-54*4)/3;
    for (int i = 0; i < titles.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(16+(68)*i, 67, 68, 77);
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [bgView addSubview:button];
        [button HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextBottom imageTitleSpace:6];
        button.tag = i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)buttonClick:(UIButton *)sender {
    if (sender.tag == 0) {
//        HNSH_ExchangeHuaFeiViewController *vc = [[HNSH_ExchangeHuaFeiViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
        ST_BaseWebController * vc = [ST_BaseWebController new];
        vc.titleString = @"";
        NSLog(@"ssssssssssssssss ==> %@",[NSString stringWithFormat:@"%@%@&appToken=%@",k_user_home_HF,[ST_UserManager shareInstance].other_token,[ST_UserManager shareInstance].access_token]);
        [vc loadURLString:[NSString stringWithFormat:@"%@%@&appToken=%@",k_user_home_HF,[ST_UserManager shareInstance].other_token,[ST_UserManager shareInstance].access_token]];
        [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
    } else if (sender.tag == 1) {
        ST_BaseWebController * vc = [ST_BaseWebController new];
        vc.titleString = @"";
        NSLog(@"ssssssssssssssss ==> %@",[NSString stringWithFormat:@"%@%@&appToken=%@",k_user_home_HF2,[ST_UserManager shareInstance].other_token,[ST_UserManager shareInstance].access_token]);
        [vc loadURLString:[NSString stringWithFormat:@"%@%@&appToken=%@",k_user_home_HF2,[ST_UserManager shareInstance].other_token,[ST_UserManager shareInstance].access_token]];
        [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
//        HNSH_ExchangeYouKaViewController *vc = [[HNSH_ExchangeYouKaViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
    } else if (sender.tag == 2) {
        ST_BaseWebController * vc = [ST_BaseWebController new];
        vc.titleString = @"";
        NSLog(@"ssssssssssssssss ==> %@",[NSString stringWithFormat:@"%@%@&appToken=%@",k_user_home_DF,[ST_UserManager shareInstance].other_token,[ST_UserManager shareInstance].access_token]);
        [vc loadURLString:[NSString stringWithFormat:@"%@%@&appToken=%@",k_user_home_DF,[ST_UserManager shareInstance].other_token,[ST_UserManager shareInstance].access_token]];
        [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
//        HNSH_ExchangeYouKaViewController *vc = [[HNSH_ExchangeYouKaViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
    } else if (sender.tag == 3) {
        HNSH_ExchangeYouKaViewController *vc = [[HNSH_ExchangeYouKaViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark networking
- (void)loadHomeScrollData {
    [HNSH_ExchangeNetServer requestJFHLMainConfigWithKey:@"third_recharge_product" success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSArray *bannerAry = [NSArray yy_modelArrayWithClass:[HNSH_HomeViewBannerItem class] json:responseObject.resultset];
            NSMutableArray *ary = [NSMutableArray arrayWithCapacity:0];
            for (HNSH_HomeViewBannerItem *item in bannerAry) {
                [ary addObject:item.image_url];
            }
            self.cycleScrollView.imageURLStringsGroup = ary;
            [self.cycleScrollView reloadInputViews];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (UIButton *)rightItemButton {
    if (!_rightItemButton) {
        _rightItemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightItemButton.frame = CGRectMake(kScreenWidth-88-20, kStatusBarHeight, 88, 44);
        [_rightItemButton setTitle:@"历史记录" forState:UIControlStateNormal];
        [_rightItemButton setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        _rightItemButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        __weak __typeof(&*self)weakSelf = self;
        [[_rightItemButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            HNSH_ExchangeRecordViewController *vc = [[HNSH_ExchangeRecordViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _rightItemButton;
}

- (SDCycleScrollView *)cycleScrollView{
    if (!_cycleScrollView) {
        _cycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectZero];
        _cycleScrollView.delegate = self;
        _cycleScrollView.placeholderImage = [UIImage imageNamed:@"icon_place"];
        _cycleScrollView.autoScrollTimeInterval = 3.0;
        _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
        _cycleScrollView.titleLabelBackgroundColor = [UIColor clearColor];
        _cycleScrollView.backgroundColor = [UIColor grayColor];
    }
    return _cycleScrollView;
}

@end
